using UnityEngine;
using QFramework;

namespace Survivor
{
    public partial class GetAllExp : ViewController
    {
        private void OnTriggerEnter2D(Collider2D other)
        {
            if (other.GetComponent<CollectableArea>())
            {
                AudioKit.PlaySound("GetAllExp");
                gameObject.DestroySelfGracefully();
                var exps = FindObjectsOfType<Exp>();
                foreach (var exp in exps)
                {
                    Player player = Player.Instance;
                    if (player)
                    {
                        ActionKit.OnUpdate.Register(() =>
                            {
                                var dir = player.transform.position - exp.transform.position;
                                exp.transform.Translate(dir.normalized * (Time.deltaTime * 5f));
                            })
                            .UnRegisterWhenGameObjectDestroyed(exp);
                    }
                }
            }
        }
    }
}